package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.sky.dto.SetmealPageQueryDTO;

import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.AddSetmealException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional
    public void addSetmeal(SetmealVO setmealVO) {
        try {
            Setmeal setmeal = new Setmeal();
            BeanUtils.copyProperties(setmealVO, setmeal);
            setmealMapper.insertSetmeal(setmeal);
            List<SetmealDish> setmealDishList = setmealVO.getSetmealDishes();
            if (setmealDishList != null && setmealDishList.size() > 0) {
                setmealDishList.forEach(setmealDish -> {
                    setmealDish.setSetmealId(setmeal.getId());
                });
                setmealDishMapper.insertSetmealDishes(setmealDishList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new AddSetmealException(e.getCause().getMessage());
        }
    }

    @Override
    public PageResult pageSetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> setmealVoPage = setmealMapper.pageSetmeal(setmealPageQueryDTO);
        PageResult pageResult = new PageResult(setmealVoPage.getTotal(), setmealVoPage.getResult());
        return pageResult;
    }

    @Override
    public void updateSetmealStatus(Long id, Integer status) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.updateSetmealStatus(setmeal);
    }

    @Override
    public SetmealVO querySetmealById(Long id) {
        SetmealVO setmealVo = setmealDishMapper.querySetmealById(id);
        List<SetmealDish> setmealDish = setmealDishMapper.selectDishesBySetmealId(id);
        setmealVo.setSetmealDishes(setmealDish);
        return setmealVo;
    }

    @Override
    public void updateSetmealById(SetmealVO setmealVO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO, setmeal);
        setmealMapper.updateSetmealById(setmeal);
        List<SetmealDish> setmealDishList = setmealVO.getSetmealDishes();
        if (setmealDishList != null && setmealDishList.size() > 0) {
            setmealDishMapper.deleteSetmealDishes(setmeal.getId());
            setmealDishList.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
            setmealDishMapper.insertSetmealDishes(setmealDishList);
        }
    }

    @Override
    public void deleteSetmealByIds(Long[] ids) {
        setmealDishMapper.deleteSetmealDishesByIds(ids);
        setmealMapper.deleteSetmealByIds(ids);
    }
}
